var record = Ext.data.Record.create([
	{name:'id', type:'string'},
	{name:'name', type:'string'},
	{name:'descn', type:'string'}
])
var combodata = [
	['0','xx'],
	['1','yy'],
	['3','zz']
]
var cm = new Ext.grid.ColumnModel([
	{header:'编号', dataIndex:'id', editor:new Ext.grid.GridEditor(new Ext.form.NumberField({//只允许使用数字
		allowBlank:false,
		allowNegative:false,//不允许负数
		maxValue:10
	}))},
	/*{header:'名称', dataIndex:'name', editor:new Ext.grid.GridEditor(new Ext.form.TextField({
		allowBlank:false
	}))},*/
	{header:'名称', dataIndex:'name', editor:new Ext.grid.GridEditor(new Ext.form.ComboBox({
		allowBlank:false,
		store:new Ext.data.SimpleStore({
			fields:['value','text'],
			data:combodata
		}),
		emptyText:'请选择...',
		mode:'local',
		triggerAction:'all',
		valueField:'value',
		displayField:'text',
		readyOnly:true
		})),
		renderer:function(value) {
			//alert(value);
			return value;
		}
	},
	{header:'描述', dataIndex:'descn', editor:new Ext.grid.GridEditor(new Ext.form.DateField({
		allowBlank:false,
		format:'Y-m-d',
		minValue:'1988-1-01-08',
		disabledDays:[0,6],
		disabledDaysText:'只能选择工作日'
	}))}
])

var ds = new Ext.data.Store({
	proxy:new Ext.data.HttpProxy({url:'grid.jsp'}),
	reader:new Ext.data.JsonReader({
		totalProperty:'totalProperty',
		root:'root'
	}, [
		{name:'id'},
		{name:'name'},
		{name:'descn'}
	])
})


Ext.onReady(function(){

	var grid = new Ext.grid.EditorGridPanel({
		store:ds,
		cm:cm,
		sm:new Ext.grid.RowSelectionModel({singleSelect:false}),
		renderTo:Ext.getBody(),
		autoHeight:true,
		//clicksToEdit:1//默认双击编辑，可以修改
		tbar:['-',{
			text:'添加一行',
			handler:function() {
				var initvalue = {id:'', name:'', descn:''};
				var p = new record(initvalue);
				
				grid.stopEditing();
				ds.insert(0,p);
				grid.startEditing(0,0);
				p.dirty = true;
				p.modified = initvalue;
				if(ds.modified.indexOf(p) == -1) {
					ds.modified.push(p)
				}
			}
		},'-',{
			text:'删除一行',
			handler:function() {
				Ext.Msg.confirm('警告','确定要删除?',function(btn){
					if(btn == 'yes') {
						var sm = grid.getSelectionModel();//用grid选择模型
						var cell = sm.getSelectedCell();//从模型中选择单元格
						//第一个是行号，第二个是列号
						var r = ds.getAt(cell[0]);
						ds.remove(r)
					}
				})
			}

		},{
			text:'保存',
			handler:function(){
				/**
				 * ds.modified就是经过修改的数组了,使用slice是对数组进行切割,
				 * 0表示从第一个元素一直切割到最后,也相当于复制了一个数组
				 */
				var m = ds.modified.slice(0);
				var jsonArray = [];
				Ext.each(m, function(item) {
					jsonArray.push(item.data);
				})
				
				Ext.lib.Ajax.request(
					'POST',
					'grid2.jsp',
					{
						success:function(response) {
							Ext.Msg.alert('信息', response.responseText, function() {
								ds.reload();
							});
						},
						failuer:function() {
							Ext.Msg.alert('错误', '与后台无法交互');
						}
					},
					'data=' + encodeURIComponent(Ext.encode(jsonArray))
				)
			}
		}]
	})
	grid.on('click',function() {
		var selections = grid.getSelectionModel().getSelections();
		for(var i = 0; i < selections.length; i++) {
			var r = selections[i];
			Ext.Msg.alert('提示', r.get('name') + ", " + selections.length)
		}
	})
	/**
	 * 如果配置了分页工具条，ds.load()必须在构造grid以后才能执行，否则
	 * 分页工具条将不会起作用
	 */
	ds.load({params:{start:0,limit:10}});//对数据初始化
	
})